$(function () {
    //var inWexin = Magiclamp.inWeixin();
    //if (inWexin) {
      // Magiclamp.mcAlert('', '请用其他扫码工具，或者用浏览器打开！');
   // }
    Magiclamp.quantityCtrl();
    /**
     *加入购物车
     */
    $("#addcart").on('click', function (e) {
        var pdata = {};
        var count_sum = parseInt($("#count-sum").html());

        var url = $(this).attr('data-href') + '/' + count_sum;//$(this).attr('data-href')
        var _this = $(this);
        var timer = null;
        if ($('#mid-toast').not(':visible')) {
            Magiclamp.midLoadingShow('加入中...');
        }
        $('#mid-toast').hide();
        $("#cartnum").removeClass('animated bounce ');
        $(this).css('pointer-events', 'none');
        Magiclamp.mcAjax(url, 'POST', pdata,
            function (data) {
                Magiclamp.midLoadingHide();
                Magiclamp.midToast('success', data.message, function () {
                    _this.css('pointer-events', 'auto');
                });
                $("#cartnum").html(parseInt(data.data.cartnum)).addClass('animated bounce ');
                if (data.error_code == 'TokenAccessErr') {
                    location.reload();
                }
            },
            function (data) {
                Magiclamp.midLoadingHide();
                Magiclamp.midToast('fail', '加入失败，请重试', function () {
                    _this.css('pointer-events', 'auto');
                });
            }
        );
        return false;
    });
    /*
     *合并购买
     * */
    $("#together_buy").on('click', function () {
        var _this = $(this);
        $(this).css('pointer-events', 'none');
        var data = $("#together_buy_form").serializeArray();
        var url = $("#together_buy_form").attr('action');
        Magiclamp.mcAjax(url, 'POST', data,
            function (data) {
                _this.css('pointer-events', 'auto');
                if (parseInt(data.status) === 1) {
                    location.href = "/entrance/cart/index";
                } else {
                    _this.css('pointer-events', 'auto');
                    Magiclamp.midToast('', data.message);
                    if (data.error_code == 'TokenAccessErr') {
                        location.reload();
                    }
                }
                return false
            },
            function () {
                Magiclamp.midLoadingHide();
                Magiclamp.midToast('fail', '购买失败，请重试', function () {
                    _this.css('pointer-events', 'auto');
                });
            });
        return false;
    });
    //猜你喜欢动态计算总价
    like_total_price();
    function like_total_price() {
        var total_money = 0;
        $('.like-price-value').each(function (i, obj) {
            if ($(obj).prev().is(":checked")) {
                total_money += parseInt($(obj).val());
            }
        });
        total_money = total_money / 100;
        $("#like-price-total").html(total_money.toFixed(2));
    }

    $(".like-checkbox").on('click', function () {
        like_total_price();
    });
    //立即购买
    $("#buy_now").on('click', function () {
        var _this = $(this);
        $(this).css('pointer-events', 'none');
        Magiclamp.midLoadingShow('处理中...');
        var count_sum = parseInt($("#count-sum").html());
        var url = $(this).attr('data-href') + '/' + count_sum;
        Magiclamp.mcAjax(url, 'POST', '', function (data) {
                _this.css('pointer-events', 'auto');
                if (data.status == 1) {
                    _this.css('pointer-events', 'auto');
                    Magiclamp.midLoadingHide();
                    var paycode = data.data.paycode;
                    location.href = "/pay/main/paytype/" + paycode;
                } else {
                    _this.css('pointer-events', 'auto');
                    Magiclamp.midLoadingHide();
                    Magiclamp.midToast('', data.message);
                }
            },
            function () {
                Magiclamp.midLoadingHide();
                Magiclamp.midToast('fail', '加入失败，请重试', function () {
                    _this.css('pointer-events', 'auto');
                });
            });
        return false;
    });
    //tab-bar增加class
    $('#sd-tab').on('click', function () {
        $('.other-tab span').removeClass('tag-on');
        $(this).addClass('tag-on');
        $('.sd-goods').show();
        $('.dn-goods').hide();
        Magiclamp.imgSrc();
    });
    $('#dn-tab').on('click', function () {
        $('.other-tab span').removeClass('tag-on');
        $(this).addClass('tag-on');
        $('.sd-goods').hide();
        $('.dn-goods').show();
        Magiclamp.imgSrc();
    });
    /**
     *神灯商品和店内商品点击加载更多方法
     * @param obj 获取更多按钮
     * @param listWrap 填充的列表  神灯列表“$('.sd-goods')”和“店内"$('.dn-goods')"
     */
    var listGetMore = function (obj, listWrap) {
        var pageNum = obj.data('page');
        var getUrl = obj.data('href') + pageNum;
        var pdata = {};
        Magiclamp.listLoad(obj);
        Magiclamp.mcAjax(getUrl, "post", pdata, function (dataBack) {
                //生成html
                var html = '';
                if (dataBack.data) {
                    dataBack.data.forEach(function (tmp) {
                        html += '<li>';
                        html += '<a href="' + tmp.url + '" class="img-wrap" >';
                        html += '<img data-src="' + tmp.preimg + '" src="/img/gray.gif">';
                        html += '</a>';
                        html += '<p class="g-l-name">';
                        html += tmp.goodstitle;
                        html += '</p>';
                        html += '<p class="g-l-price">';
                        html += '<span>￥</span>';
                        html += '<span class="price">' + ((tmp.price / 100 ).toFixed(2)) + '</span>';
                        html += '</p>';
                        html += '</li>';
                    })
                }
                //插入ul
                listWrap.find('ul').append(html);
                Magiclamp.listLoadGet(obj, '加载更多', 'auto');
                Magiclamp.imgSrc();
                obj.data('page', Number(pageNum) + 1);
                //如果数据少于9条,说明已加载全部
                if (dataBack.data.length < 9) {
                    Magiclamp.listLoadGet(obj, '已加载全部', 'none');
                    return false;
                }
            }, function (databack) {
                Magiclamp.listLoadGet(obj, databack.message, 'none');
            }, true
        );
    };
    $('#sd-getmore').on('click', function () {
        listGetMore($('#sd-getmore'), $('.sd-goods'));
        return false;
    });
    $('#dn-getmore').on('click', function () {
        listGetMore($('#dn-getmore'), $('.dn-goods'));
        return false;
    });
    Magiclamp.tabAddCls('self_service');
});
